import json
from django.http import JsonResponse
from django.shortcuts import render, redirect
from book.models import Book
from author.models import Author
from publish.models import Publish
from django.views import View
from utils.MyPage import Pagination


def detail_book(request, *args, **kwargs):
    current_page = request.GET.get('page', 1)
    all_count = Book.objects.all().count()
    book_data_all = Book.objects.all()
    # book_data = Book.objects.all()
    page_obj = Pagination(current_page=current_page, all_count=all_count, per_page_num=5, pager_count=5)
    book_data = book_data_all[page_obj.start:page_obj.end]
    page_html = page_obj.page_html()
    return render(request, 'book_main.html', context=locals())


class AddBook(View):
    def get(self, request, *args, **kwargs):
        publish_all = Publish.objects.all()
        author_all = Author.objects.all()
        return render(request=request, template_name='book_add.html', context=locals())

    def post(self, request, *args, **kwargs):
        data = request.POST
        # 获得除作者的id的其他书籍信息
        book_data = request_book_data(data)
        # 获得作者id
        author_id = data.getlist('author_id')
        # 将书籍信息录入数据库
        book_obj = Book.objects.create(**book_data)
        # 设置多对多关系
        book_obj.author.set(author_id)
        return redirect(to='book:detail')


def request_book_data(data):
    book_data = {
        'name': data.get('title'),
        'price': data.get('price'),
        'image': str(data.get('image')),
        'desc': data.get('desc'),
        'publish_id': data.get('publish_id'),
        'url': data.get('detail_url'),
        'word_count': data.get('word_count'),
        'register_date': data.get('register_date'),
    }
    return book_data


def del_book(request, *args, **kwargs):
    if request.is_ajax():
        try:
            json_code = request.body
            data = json.loads(json_code)
            bid = data.get('bid')
            Book.objects.filter(pk=bid).delete()
        except Exception as e:
            return JsonResponse({'error': e})
    return redirect(to='book:detail')


class RenewBook(View):
    def get(self, request, bid, *args, **kwargs):
        book_obj = Book.objects.get(pk=bid)
        book2author_obj = book_obj.author.all()
        author_all = Author.objects.all()
        publish_all = Publish.objects.all()
        book2publish_obj = book_obj.publish
        # return render(request=request, template_name='book_renew.html', context=locals())
        return render(request=request, template_name='book_renew.html', context=locals())

    def post(self, request, *args, **kwargs):
        data = request.POST
        bid = int(request.get_full_path().split('/')[-1])
        book_obj = Book.objects.get(pk=bid)
        # 获得除作者的id的其他书籍信息
        book_data = request_book_data(data)
        # 获得作者id
        author_id = data.getlist('author_id')
        # 将书籍信息录入数据库
        Book.objects.filter(pk=bid).update(**book_data)
        # 设置多对多关系
        book_obj.author.set(author_id)
        return redirect(to='book:detail')
